/*******************************************************************************
 * 文件名: PdfExport.java
 * 描    述: 导出PDF
 * 创建人: Dean Huang(hudi-112@163.com)
 * 日　期: 2018-01-11
 * 描　述: 文件描述
 * 版　本: 1.0
 *******************************************************************************/

package com.z1.core.data.export.pdf;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.z1.core.data.export.pdf.model.PdfTable;
import com.z1.core.data.export.pdf.model.PdfTableCell;
import com.z1.core.data.export.pdf.model.PdfTableRow;

public class PdfExport {

	//字体,支持中文
	private Font font = null;
	//文档
	private Document document = null;
	//PDF文档流
	private ByteArrayOutputStream stream = null;
	
	public PdfExport() throws Exception {
		this(null, null, null, null);
	}
	
	public PdfExport(String title) throws Exception {
		this(title, null, null, null);
	}
	
	public PdfExport(String title, String subject) throws Exception {
		this(title, subject, null, null);
	}
	
	public PdfExport(String title, String subject, String keywords) throws Exception {
		this(title, subject, keywords, null);
	}
	
	public PdfExport(String title, String subject, String keywords, String author) throws Exception {
		//A4纸张，上下左右边距
		document = new Document(PageSize.A4, 40, 40, 40, 40);
		stream = new ByteArrayOutputStream();
		PdfWriter.getInstance(document, stream);
//		PdfWriter pdfWriter = PdfWriter.getInstance(document, stream);
//		pdfWriter.setEncryption(Certificate[] certs, final int[] permissions, final int encryptionType
		//设置标题
		if (null != title) { document.addTitle(title); }
		//设置主题
		if (null != subject) { document.addSubject(subject); }
		//设置关键字
		if (null != keywords) { document.addKeywords(keywords); }
		//设置作者
		if (null != author) { document.addAuthor(author); }
		//打开文档
		document.open();
		
		//字体
		BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		font = new Font(baseFont, 12, Font.NORMAL);
	}
	
	public void addText(String text) throws Exception {
		if (null != document && document.isOpen()) {
			document.add(new Paragraph(text, font));
		}
	}
	
	/**
	 * 设置表格
	 * @param heads
	 * @param values
	 * @throws Exception
	 */
	public void addTable(PdfTable pdfTable) throws Exception {
		if (null != document && document.isOpen()) {
			//Table
			if (null != pdfTable && null != pdfTable.getHead()) {
				PdfPTable table = new PdfPTable(pdfTable.getWidths());
//				table.setSpacingBefore(10F);
//				table.setSpacingAfter(10F);
				table.setWidthPercentage(100F);
				
				for (PdfTableCell pdfTableCell : pdfTable.getHead().getCells()) {
					PdfPCell cell = new PdfPCell(
						new Paragraph(pdfTableCell.getData(),  font)
					);
					if (pdfTableCell.getColspan() > 1) {
						cell.setColspan(pdfTableCell.getColspan());
					}
					if (pdfTableCell.getRowspan() > 1) {
						cell.setRowspan(pdfTableCell.getRowspan());
					}
					table.addCell(cell);
				}
				
				for (PdfTableRow pdfTableRow : pdfTable.getRows()) {
					for (PdfTableCell pdfTableCell : pdfTableRow.getCells()) {
						PdfPCell cell = new PdfPCell(
								new Paragraph(pdfTableCell.getData(),  font)
								);
						if (pdfTableCell.getColspan() > 1) {
							cell.setColspan(pdfTableCell.getColspan());
						}
						if (pdfTableCell.getRowspan() > 1) {
							cell.setRowspan(pdfTableCell.getRowspan());
						}
						table.addCell(cell);
					}
				}
				document.add(table);
			}
		}
	}
	
	/**
	 * 设置图片
	 * @param imgFilePath
	 * @throws Exception
	 */
	public void addImage(String imgFilePath) throws Exception {
		document.add(Image.getInstance(imgFilePath));
	}
	/**
	 * 设置图片
	 * @param buf
	 * @throws Exception
	 */
	public void addImage(byte[] buf) throws Exception {
		document.add(Image.getInstance(buf));
	}
	/**
	 * 设置图片
	 * @param image
	 * @throws DocumentException
	 */
	public void addImage(Image image) throws DocumentException {
		document.add(image);
	}
	
	/**
	 * 关闭文档
	 */
	public void close() {
		if (null != document && document.isOpen()) {
			//关闭文档
			document.close();
		}
	}
	
	/**
	 * 输出至流
	 * @param output
	 * @throws Exception
	 */
	public void export(OutputStream output) throws Exception {
		close();
		if (null != stream) {
			stream.writeTo(output);
		}
	}
	
}
